<?php

namespace App\Http\Controllers\Api\v4;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\BaseController;
use App\Models\Api\User;
use App\Models\CacheModel\zl2\AstroFortuneChange as AstroFortuneChangeModel;

class AstroFortuneChangeController extends BaseController
{
    /**
     * 星运变动     首页
     * @return Json
     * @throws \Exception
     */
    public function index(Request $request)
    {
        $date = $request->input('date', '');
        $type = $request->input('type', 1);
        $userinfo = (new User())->getUserinfo(['id'=>$request->uid]);
        // if (!in_array($type, [1, 2, 3], true))
        // {
        //     return show(config('code.error'), '请求异常，请稍后再试～');
        // }
        if (!$userinfo && in_array($type, [2, 3], true))
        {
            return show(config('code.login'), '前往登录');
        }
        if ($date && $date !== (date("Y-m-d", strtotime($date))))
        {
            return show(config('code.error'), '日期格式异常，请稍后再试～');
        }

        if (in_array($type, [2, 3], true) && $userinfo['rid'] <= 0)
        {
            return show(406, '档案缺失');
        }

        $resData = (new AstroFortuneChangeModel())->getRecodeAstroFortuneChange($userinfo['id'] ?: 0, $userinfo['rid'], $date, $type);
        return show(config('code.success'), '请求成功', $resData);
    }
}
